package com.sdr.locationservice;

import android.Manifest;
import android.content.Intent;
import android.os.Build;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * Created by homelajiang on 2016/9/1 0001.
 */
public class LocUploadManager extends CordovaPlugin {
    String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    private Intent intent;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("init")) {
            intent = new Intent(this.cordova.getActivity(), LocationService.class);
            intent.putExtra("userId", args.getString(0));
            intent.putExtra("accessToken", args.getString(1));
            intent.putExtra("url", args.getString(2));
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                cordova.requestPermissions(this, 0, permissions);
            } else {
                this.cordova.getActivity().startService(intent);
            }
            return true;
        }
        if (action.equals("stop")) {
            intent = new Intent(this.cordova.getActivity(), LocationService.class);
            intent.putExtra("stop", true);
            cordova.getActivity().startService(intent);
        }
        return false;
    }

    @Override
    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {

        this.cordova.getActivity().startService(intent);
    }
}
